package com.example.exceldemo.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.example.exceldemo.listener.EasyExcelListener;
import com.example.exceldemo.resp.DynamicCellResp;
import com.example.exceldemo.resp.ProductResp;
import com.example.exceldemo.service.CrossImpExcelService;
import com.example.exceldemo.service.easyIpmExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author xiaohuang
 * @Date 2025/2/21
 */
@Api(tags = "easyExcel导入")
@RequestMapping("easy/imp")
@RestController
public class EasyExcelIpmController {
    @Autowired
    private easyIpmExcelService excelService;
    @Autowired
    private CrossImpExcelService crossImpExcelService;
    @ApiOperation("easyexcel注解反射解析基本信息+表单(复杂可用)")
    @PostMapping(value = "easy/merge")
    public ProductResp saveExcelInfo(@ApiParam(name = "file", value = "file", required = true) @RequestParam(value = "file") MultipartFile file) throws IOException {
        ProductResp productResp = excelService.saveExcelInfo(file);
        return productResp;
    }
    @ApiOperation("easyexcel注解解析多sheet合并单元格(复杂可用)")
    @PostMapping(value = "easy/sheet/merge")
    public ProductResp upload(@ApiParam(name = "file", value = "file", required = true) @RequestParam(value = "file") MultipartFile file) throws IOException {
        // 读取excel内容
        EasyExcelListener easyExcelListener = new EasyExcelListener();
        ExcelReaderBuilder read = EasyExcelFactory.read(file.getInputStream(), easyExcelListener);
        ExcelReader excelReader = read.build();
        // 获取各个sheet页信息
        List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
        // 获取各个Shhet页表格内容存于map
        Map<Integer, List<LinkedHashMap>> sheetInfos = new HashMap<>(sheets.size());
        for (ReadSheet sheet : sheets) {
            Integer sheetNo = sheet.getSheetNo();
            excelReader.read(sheet);
            sheetInfos.put(sheetNo, easyExcelListener.getListMap());
        }
        ProductResp productResp = crossImpExcelService.saveExcelInfo(sheetInfos);
        return productResp;
    }
    /**
     * 读取单元格带图片
     */
    @ApiOperation("easy监听器动态解析合并单元格excel")
    @PostMapping("dynamicCell")
    public DynamicCellResp dynamicCell(@NotNull(message = "未选择上传的文件") @RequestParam("file") MultipartFile file) {
        DynamicCellResp dynamicCellResp=excelService.dynamicCell(file);
        return dynamicCellResp;
    }



}
